package com.aric.springbootmusic.service.impl;

import com.aric.springbootmusic.entity.pojo.User;
import com.aric.springbootmusic.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User user = userMapper.selectOne(lqw);
        if(Objects.isNull(user)){
            throw new UsernameNotFoundException("用户名没有找到");
        }

        List<String> permissions=userMapper.selectPermissionsByUserId(user.getId());
        return new UserDetailsImpl(user,permissions);
    }
}
